<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\OrderRepo;
use App\Repos\ShopRepo;
use App\Repos\ProductRepo;
use App\Models\Cate;
use App\Exports\OrderExport;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends Controller
{
    public function index(Request $request)
    {
    	$where = [];
    	
        if($request->user_id) {
            $where['user_id'] = (int)$request->user_id;
        }

        if($request->cate_id) {
            $where['cate_id'] = (int)$request->cate_id;
        }
    	$list  = OrderRepo::paginate($where);
    	
        $cates = Cate::get();
    	return view('admin.order.index',compact('list','where','cates'));
    }

    public function detail($order_id)
    {
    	$data = OrderRepo::find($order_id);
    }

    public function confirm(Request $request)
    {    
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data['order_id'] = $request->order_id;
        $flag = OrderRepo::confirm($data);
        //dd($flag);
        if($flag['status']== false) {
            $rs['msg'] = $flag['msg'];
            $rs['status'] = 'danger';
            return back()->with('rs',$rs);
        }
        return back();
    }

    /**
     * 发货
     */
    public function send(Request $request)
    {    
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data = $request->except(['_token']);
        $flag = OrderRepo::send($data);
        //dd($flag);
        if($flag['status']== false) {
            $rs['msg'] = $flag['msg'];
            $rs['status'] = 'danger';
            return back()->with('rs',$rs);
        }
        return back();
    }

    public function cancle(Request $request)
    {    
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data['order_id'] = $request->order_id;
        $flag = OrderRepo::cancle($data);
        //dd($flag);
        if($flag['status']== false) {
            $rs['msg'] = $flag['msg'];
            $rs['status'] = 'danger';
            return back()->with('rs',$rs);
        }
        return back();
    }

    public function reject(Request $request)
    {    
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data['order_id'] = $request->order_id;
        $flag = OrderRepo::reject($data);
        //dd($flag);
        if($flag['status']== false) {
            $rs['msg'] = $flag['msg'];
            $rs['status'] = 'danger';
            return back()->with('rs',$rs);
        }
        return back();
    }

    public function complete(Request $request)
    {    
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data['order_id'] = $request->order_id;
        $flag = OrderRepo::complete($data);
        //dd($flag);
        if($flag['status']== false) {
            $rs['msg'] = $flag['msg'];
            $rs['status'] = 'danger';
            return back()->with('rs',$rs);
        }
        return back();
    }

    /**
     * 订单统计
     */
    public function export(Request $request) 
    {   
        $param=[];     
        return Excel::download(new OrderExport($param), '订单列表.xlsx');   
    }

   
}
